package com.weharvest.edu.interceptor;

import com.weharvest.edu.annotation.SignSecurity;
import com.weharvest.edu.dao.mapper.InterceptorDaoMapper;
import com.weharvest.edu.token.Jwt;
import com.weharvest.edu.token.Token;
import com.weharvest.edu.token.TokenState;
import com.weharvest.edu.utlis.JsonUtil;
import com.weharvest.edu.utlis.ResponseUtil;
import com.weharvest.edu.vo.ResultVo;
import net.minidev.json.JSONObject;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;


/**
 * 前台token验证
 */
@Component
public class ReceptionInterceptor extends HandlerInterceptorAdapter {

    @Resource
    private InterceptorDaoMapper interceptorDaoMapper;

    /**
     * 拦截前端token请求
     *
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //表示不拦截请求
        if (!(handler instanceof HandlerMethod)) {
            return true;
        }
        HandlerMethod handlerMethod = (HandlerMethod) handler;

        //是否开启登录注解
        SignSecurity signSecurity = handlerMethod.getMethodAnnotation(SignSecurity.class);
        if (null == signSecurity) {
            //是否开启类注解
            signSecurity = AnnotationUtils.findAnnotation(handlerMethod.getBeanType(), SignSecurity.class);
            if (null == signSecurity) {
                return true;
            }
        }
        //验证是否允许权限
        JSONObject jsonObject = new JSONObject();
        String tokenValue = request.getHeader("token");
        if (null == tokenValue || tokenValue.equals("")) {
            //不合法的请求头
            jsonObject.put("code", 403);
            jsonObject.put("msg", "不合法请求");
            ResponseUtil.wirteResponseMsg(jsonObject, response);
            return false;
        }
        //检验token值
        Map<String, Object> map = Jwt.validToken(tokenValue);
        Object data = map.get("data");
        //信息不能为空
        if (null != data) {
            //获取返回信息
            ResultVo resultVo = new ResultVo();
            TokenState tokenState = TokenState.getTokenState(map.get("state").toString());
            if (null != data) {
                Token token = JsonUtil.fromJson(data.toString(), Token.class);
                return true;
            }
        } else {
            //假如就是解析token错误
            TokenState tokenState = TokenState.getTokenState(map.get("state").toString());
            jsonObject.put("code", tokenState.getCode());
            jsonObject.put("msg", tokenState.getState());
            ResponseUtil.wirteResponseMsg(jsonObject, response);
            return false;
        }
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    }

}
